<template>
  <div id="auth-page">
    Auth
    <div class="px-1">rem 40px</div>
    <div class="px-2">real 40px</div>
  </div>
</template>

<script>
import { Indicator } from 'mint-ui'
import { appConfig, GetQueries } from './../util.js'

export default {
  name: 'Auth',
  data () {
    return {
      code: null
    }
  },
  mounted () {
    Indicator.open('微信授权中...')
    this.code = GetQueries().code
    if (this.code) {
      this.getUser()
    } else {
      this.turnWx()
    }
  },
  methods: {
    turnWx () {
      let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appConfig.appid}&redirect_uri=${window.location.href.split('#')[0]}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redire`
      window.location.href = url
    },
    getUser () {
      alert(window.location.href)
      let code = this.code
      // 利用code 换取用户信息
      let data = {
        code
      }
      let beforeAuthUrl = localStorage.getItem('beforeAuthUrl')
      localStorage.removeItem('beforeAuthUrl')
      this.$store.commit('setUser', data)
      Indicator.close()
      window.location.href = beforeAuthUrl
    }
  }
}
</script>

<style lang="scss" scoped>
@import './../styles/common';

#auth-page{
  color: #fff;
  background: #666;
  .px-1{
    font-size: px2rem(40px);
  }
  .px-2{
    @include font-dpr(40px);
  }
}
</style>
